package com.lv.service.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.imp.OnPositionTListener
import com.lv.common.imp.OnPositionTMore
import com.lv.service.R
import com.lv.service.adapter.TextTagSelectedMoreAdapter
import com.lv.service.bean.CommonSelectedBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class Recycler2SelectedMorePopupView(
    private val mContext: Context,
    private val mTitle: String,
    private val mFristBeans: MutableList<CommonSelectedBean>,
    private val mSecoundBeans: MutableList<CommonSelectedBean>,
    private var topH: Int = 0,
    private val mTopOnlyOneEnable: Boolean = false,
    private val mBottomonlyOneEnable: Boolean = false,
    private val mLayout: Int = R.layout.recycler2_selected_popup,
    private val mSelectedTag: Int = com.lv.common.R.drawable.bg_line_eb613d_slid_10_16,
    private val mSelectedColor: Int = Color.parseColor("#242424"),
    private val mPositionMListener: OnPositionTMore<List<CommonSelectedBean>>? = null
) :
    BottomPopupView(mContext) {
    private var rvTop: RecyclerView? = null
    private var rvBottom: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var tvTitleTop: TextView? = null
    private var tvTitleBottom: TextView? = null
    private var ivClose: ImageView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private var mTopBeans = mutableListOf<CommonSelectedBean>()
    private var mBottomBeans = mutableListOf<CommonSelectedBean>()

    private var mTopAdapter: TextTagSelectedMoreAdapter? = null
    private var mBottomAdapter: TextTagSelectedMoreAdapter? = null
    override fun getImplLayoutId(): Int {
        return mLayout
    }

    override fun onCreate() {
        super.onCreate()
        rvTop = findViewById<RecyclerView>(R.id.rv_top)
        rvBottom = findViewById<RecyclerView>(R.id.rv_bottom)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvTitleTop = findViewById<TextView>(R.id.tv_title_1)
        tvTitleBottom = findViewById<TextView>(R.id.tv_title_2)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initAdapter()
        initEvnet()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initAdapter() {
        mTopBeans.clear()
        mBottomBeans.clear()
        mFristBeans.forEach {
            mTopBeans.add(CommonSelectedBean(it.name, it.tag, it.selected))
        }
        mSecoundBeans.forEach {
            mBottomBeans.add(CommonSelectedBean(it.name, it.tag, it.selected))
        }


        val gridLayoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvTop?.layoutManager = gridLayoutManager
        mTopAdapter = TextTagSelectedMoreAdapter(
            mTopBeans, selectedTag = mSelectedTag, selectedColor = mSelectedColor
        )
        rvTop?.adapter = mTopAdapter
        mTopAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (mTopOnlyOneEnable) {
                val index = mTopBeans.indexOfFirst { it.selected }
                if (index != -1 && position == index) {
                    return@OnPositionTListener
                }
                mTopBeans.forEachIndexed { indexForEach, commonSelectedBean ->
                    if (commonSelectedBean.selected) {
                        commonSelectedBean.selected = false
                        mTopAdapter?.notifyItemChanged(indexForEach, R.id.tv_name)
                    }
                }
                val selectedPosition = mTopBeans[position].selected
                mTopBeans[position].selected = !selectedPosition
                mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
//                val index = mTopBeans.indexOfFirst { it.selected }
//                if (index == -1) {
//                    val selectedPosition = mTopBeans[position].selected
//                    mTopBeans[position].selected = !selectedPosition
//                    mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
//                } else {
//                    if (index == position) {
//                        val selectedPosition = mTopBeans[position].selected
//                        mTopBeans[position].selected = !selectedPosition
//                        mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
//                    } else {
//                        mTopBeans[index].selected = false
//                        mTopAdapter?.notifyItemChanged(index, R.id.tv_name)
//                        mTopBeans[position].selected = true
//                        mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
//                    }
//                }
            } else {

                val selectedPosition = mTopBeans[position].selected
                mTopBeans[position].selected = !selectedPosition
                mTopAdapter?.notifyItemChanged(position, R.id.tv_name)


//                val selected = mTopBeans[0].selected
//                if (position == 0) {
//                    mTopBeans.forEach { item -> item.selected = false }
//                    mTopBeans[0].selected = !selected
//                    mTopAdapter?.notifyDataSetChanged()
//                } else {
//                    if (selected) {
//                        mTopBeans[0].selected = false
//                        mTopAdapter?.notifyItemChanged(0, R.id.tv_name)
//                    }
//                    val selectedPosition = mTopBeans[position].selected
//                    mTopBeans[position].selected = !selectedPosition
//                    mTopAdapter?.notifyItemChanged(position, R.id.tv_name)
//                }
            }
        }
        val gridLayoutManagerBottom = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rvBottom?.layoutManager = gridLayoutManagerBottom
        mBottomAdapter = TextTagSelectedMoreAdapter(
            mBottomBeans, selectedTag = mSelectedTag, selectedColor = mSelectedColor
        )
        rvBottom?.adapter = mBottomAdapter
        mBottomAdapter?.onViewClickListener = OnPositionTListener { position, data ->
            if (mBottomonlyOneEnable) {
                val index = mBottomBeans.indexOfFirst { it.selected }
                if (index != -1 && position == index) {
                    return@OnPositionTListener
                }
                mBottomBeans.forEachIndexed { indexForEach, commonSelectedBean ->
                    if (commonSelectedBean.selected) {
                        commonSelectedBean.selected = false
                        mBottomAdapter?.notifyItemChanged(indexForEach, R.id.tv_name)
                    }
                }
                val selectedPosition = mBottomBeans[position].selected
                mBottomBeans[position].selected = !selectedPosition
                mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)

            } else {
                val selectedPosition = mBottomBeans[position].selected
                mBottomBeans[position].selected = !selectedPosition
                mBottomAdapter?.notifyItemChanged(position, R.id.tv_name)
            }
        }
    }

    private fun initView() {
        tvTitle?.text = mTitle

    }

    private fun initEvnet() {
        viewTop?.setOnClickListener {
            dismiss()
        }
        ivClose?.setOnClickListener {
            dismiss()
        }

        viewShadow?.setOnClickListener {
            mPositionMListener?.onClick(0, mTopBeans, mBottomBeans)
            dismiss()
        }
    }

    fun setTitleTop(text: String): Recycler2SelectedMorePopupView {
        tvTitleTop?.text = text
        return this
    }

    fun setTitleBottom(text: String): Recycler2SelectedMorePopupView {
        tvTitleBottom?.text = text
        return this
    }

    fun setBottomBg(bg: Int): Recycler2SelectedMorePopupView {
        viewShadow?.setBackgroundResource(bg)
        return this
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}